package com.cet.qz.unit01.chapter1_2;

import java.time.DayOfWeek;
import java.time.LocalDate;

/**
 * @program: algorithm
 * @description:
 * @author: 陈恩涛
 * @create: 2021-03-04 10:26
 **/
public class Qz1_2_11 {

    public static void main(String[] args) {
        SmartDate smartDate = new SmartDate("2019/01/05");
        System.out.println(smartDate);
        System.out.println(smartDate.getDayOfWeek());
    }
}

class SmartDate {
    private final int year;
    private final int month;
    private final int day;

    public SmartDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
        if (!islegal()) {
            throw new RuntimeException("日期不合法！");
        }
    }

    public SmartDate(String date) {
        String[] dates = date.split("/");
        this.year = Integer.parseInt(dates[0]);
        this.month = Integer.parseInt(dates[1]);
        this.day = Integer.parseInt(dates[2]);

        if (!islegal()) {
            throw new RuntimeException("日期不合法！");
        }
    }

    private boolean islegal()
    {
        if(year<=0 || month<=0 || month>12 || day<=0)
        {
            return false;
        }
        int [] days={31,28,31,30,31,30,31,31,30,31,30,31};
        /*普通闰年：公历年份是4的倍数，且不是100的倍数的，为闰年（如2004年、2020年等就是闰年）。
        世纪闰年：公历年份是整百数的，必须是400的倍数才是闰年（如1900年不是闰年，2000年是闰年）。*/
        if(year%400==0 ||(year%4==0 && year%100!=0))
        {
            //是闰年
            days[1] = 29;//闰年二月
        }
        if(day>days[month-1])
        {
            return false;
        }
        return true;
    }

    public DayOfWeek getDayOfWeek() {
        LocalDate localDate = LocalDate.of(year, month, day);
        return localDate.getDayOfWeek();
    }

    @Override
    public String toString() {
        return "SmartDate{" +
                "year=" + year +
                ", month=" + month +
                ", day=" + day +
                '}';
    }

}
